package com.example.tuike.mapper;

import com.example.tuike.common.SubscriptionStatus;
import com.example.tuike.entity.Subscription;
import org.apache.ibatis.annotations.*;

import java.util.List;

/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/1/29 20:06

 * @Version 1.0*/
@Mapper
public interface SubscriptionMapper {
    @Insert("""
        INSERT INTO subscription 
            (userId, feedId, title, status, createdAt, updatedAt)
        VALUES 
            (#{userId}, #{feedId}, #{title}, #{status}, #{createdAt}, #{updatedAt})
        """)
    @Options(useGeneratedKeys = true, keyProperty = "subscriptionId")
    void insert(Subscription subscription);

    @Select("SELECT * FROM subscription WHERE subscriptionId = #{subscriptionId}")
    Subscription findById(Integer subscriptionId);

    @Update("""
        UPDATE subscription SET
            title = #{title},
            status = #{status},
            updatedAt = #{updatedAt}
        WHERE subscriptionId = #{subscriptionId}
        """)
    void update(Subscription subscription);

    @Select("SELECT * FROM subscription WHERE userId = #{userId} AND status = #{status}")
    List<Subscription> findByUserIdAndStatus(
            @Param("userId") Integer userId,
            @Param("status") SubscriptionStatus status
    );
    @Select("SELECT * FROM subscription WHERE userId = #{userId} AND feedId = #{feedId}")
    Subscription findByUserIdAndFeedId(@Param("userId") Integer userId, @Param("feedId") Integer feedId);

    @Select("SELECT COUNT(*) FROM subscription WHERE userId = #{userId} AND feedId = #{feedId} AND status = 'ACTIVE'")
    boolean isSubscribed(@Param("userId") Integer userId, @Param("feedId") Integer feedId);
}
